package cn.tedu._05weibo.controller;

import cn.tedu._05weibo.common.response.JsonResult;
import cn.tedu._05weibo.common.response.StatusCode;
import cn.tedu._05weibo.mapper.WeiboMapper;
import cn.tedu._05weibo.pojo.dto.WeiboDTO;
import cn.tedu._05weibo.pojo.vo.WeiboIndexVO;
import cn.tedu._05weibo.pojo.entity.Weibo;
import cn.tedu._05weibo.pojo.vo.UserVO;
import cn.tedu._05weibo.pojo.vo.WeiboDetailVO;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpSession;
import java.io.Serializable;
import java.util.Date;
import java.util.List;
import java.util.concurrent.TimeUnit;

@Slf4j
@Api(tags = "02.微博管理模块")
@RestController
@RequestMapping("/v1/weibo")
@Validated
public class WeiboController {

    @Autowired
    private WeiboMapper weiboMapper;

    @PostMapping("insert")
    @ApiOperation("发布微博")
    public JsonResult insert(@RequestBody WeiboDTO weiboDTO, @ApiIgnore HttpSession session) {

        UserVO userVO = (UserVO) session.getAttribute("user");
        /*if (userVO == null) {
            return new JsonResult(StatusCode.NOT_LOGIN);
        } else {*/
            Weibo weibo = new Weibo();
            BeanUtils.copyProperties(weiboDTO, weibo);
            weibo.setCreated(new Date());
            weibo.setUserId(userVO.getId());
            weiboMapper.insertWeibo(weibo);

            return JsonResult.ok();
//        }
    }

    @Autowired
    private RedisTemplate<String, Serializable> redisTemplate;

    @GetMapping("selectIndex")
    @ApiOperation("查询首页微博")
    public JsonResult selectIndex() throws JsonProcessingException {
        ValueOperations<String, Serializable> ops = redisTemplate.opsForValue();
        Serializable weiboIndex = ops.get("weibo_index");
        if(weiboIndex == null){
            List<WeiboIndexVO> weiboIndexVOS = weiboMapper.selectWeiboIndex();
            //Jackson：序列化和反序列化的库
            ObjectMapper objectMapper = new ObjectMapper();
            String indexJson = objectMapper.writeValueAsString(weiboIndexVOS);

            //将Java对象序列化成Json格式保存到Redis中
            ops.set("weibo_index", indexJson, 30, TimeUnit.SECONDS);
            System.out.println("从Mysql数据库中查询");
            return JsonResult.ok(weiboIndexVOS);
        }
        System.out.println("从Redis中查询");
        //将Json格式的String对象反序列化成Java对象,反序列化
        ObjectMapper objectMapper = new ObjectMapper();
        List<WeiboIndexVO> indexVOList = objectMapper.readValue(weiboIndex.toString(), List.class);
        return JsonResult.ok(indexVOList);
    }

    @GetMapping("selectById")
    @ApiOperation("查询微博详情")
    @ApiImplicitParam(name = "id", value = "微博id", required = true, dataType = "int", example = "200")
//    @ApiImplicitParams(value = {
//            @ApiImplicitParam(name = "id", value = "微博id", required = true, dataType = "int"),
//            @ApiImplicitParam(name = "username", value = "用户名", required = true)
//    })
    public JsonResult selectById(@Range(min = 200, max = 500, message = "微博id必须在200~500之间") @RequestParam/*("aaa")*/ Integer id) {

        log.debug("id = " + id);
        WeiboDetailVO weiboDetailVO = weiboMapper.selectById(id);
        log.debug("weiboDetailVO = " + weiboDetailVO);

        return JsonResult.ok(weiboDetailVO);

    }

}
